<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片组件 -->
    <el-card>
      <el-alert title="注意：只允许为第三级分类设置参数" type="warning" :closable="false"> </el-alert>
      <el-row class="el-row">
        <el-col>
          <el-cascader v-model="seletedKeys" :options="catesList" :props="cascaderProps" @change="handleChange" :clearable="true"></el-cascader>
        </el-col>
      </el-row>

      <!-- 标签页组件 -->
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <el-tab-pane label="动态参数" name="many">
            <el-button type="primary" size="mini" :disabled='btnDisabled'>添加参数</el-button>
            <!-- 表格组件 -->
            <el-table :data="paramsDataList" border stripe>
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column label="参数名称" prop="attr_name"></el-table-column>
                <el-table-column label="操作">
                    <!-- 自定义模板 -->
                    <template >
                        <el-button type="primary" size="mini" icon='el-icon-edit'>编辑</el-button>
                         <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-tab-pane>

        <el-tab-pane label="静态属性" name="only">
            <el-button type="primary" size="mini" :disabled='btnDisabled'>添加属性</el-button>
            <!-- 表格组件 -->
            <el-table :data="paramsDataList" border stripe>
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column label="参数名称" prop="attr_name"></el-table-column>
                <el-table-column label="操作">
                    <!-- 自定义模板 -->
                    <template >
                        <el-button type="primary" size="mini" icon='el-icon-edit'>编辑</el-button>
                         <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'params',
  data() {
    return {
      catesList: [],
      //级联选择器选中时会把数据存在seletedKeys中
      seletedKeys: [],
      //级联选择器展示配置项
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover',
      },
      //当前激活的标签页
      activeName: 'many',
      paramsDataList:[]
    }
  },
  created() {
    this.getCatesList()
  },
  methods: {
    async getCatesList() {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) {
        return this.$message.error('获取分类数据失败！')
      }
      this.catesList = res.data
    //   console.log(this.catesList)
    },
    //监听级联选择器改变事件
    handleChange() {
        this.getParamsList()
    },
    //   监听标签页的切换
    handleTabClick() {
        this.getParamsList()
    },
    //获取分类参数
    getParamsList(){
        //判断是否选中三级分类
        if(this.seletedKeys.length !==3){
            this.seletedKeys = []
            return
        }
        //选着的是三级分类才去获取参数
        this.$http.get(`categories/${this.crentCateId}/attributes`,{
            params:{
                sel: this.activeName
            }
        }).then( res =>{
            if(res.data.meta.status !==200){
                return this.$message.error('获取参数失败！')
            }
            this.paramsDataList = res.data.data
        })
    }
  },
  computed: {
      crentCateId(){
          return this.seletedKeys.length ==3? this.seletedKeys[2] : null
      },
      btnDisabled(){
          return this.seletedKeys.length ==3? false : true
      }
  }
}
</script>

<style lang="less" scoped>
.el-row {
  margin: 15px;
}
</style>